<?php
/** 
 * @package ShareThisBar Plugin for Joomla! 1.5
 * @subpackage Form Element Stbradio
 * @version $Id: sharethisbar.php 3.5 2012-12-29 17:00:33Z Dusanka $
 * @author Dusanka Ilic
 * @copyright (C) 2012 - Dusanka Ilic, All rights reserved.
 * @authorEmail: gog27.mail@gmail.com
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html, see LICENSE.txt
**/

// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();

/**
 * Renders a radio element like standard control,
 * except in making attribute id for input element.
 */

class JElementStbRadio extends JElement
{
	var	$_name = 'StbRadio';

	function fetchElement($name, $value, &$node, $control_name)
	{
		$options = array ();
                
                // XML file defined element.
		foreach ($node->children() as $option)
		{
                        // Attribute value from element <option>.
			$val = $option->attributes('value');
                        
                        // If element value preserved in params is equal to value in element <option>
                        // that element should be checked on admin form.
                        if ($value == $val) {
                         $checked = ' checked="checked" ';   
                        } else {
                         $checked = '';   
                        }
                        
                        // data() returns value between <option> </option>.
			$text = $option->data();
                        
                        // metod select.option returns object of stdClass type.
			$options[] = JHTML::_('select.option', $val, JText::_($text));
                        
                        // The end() function moves the pointer to the last position of an array, 
                        // returning the last element. Last added array element I store in object.
                        $obj = end($options);
                        
                        // Give property checked to last object in array.
                        $obj->checked = $checked;
                        
		}
     
                // Code for rendering.
                $html = '';
                
                // For each object in array options, I make html input element and label.
                // Difference in relation to the standard radio button is in making input element id, I use $i not $value.
                // It's easier to work with such a id in javascript.
                for ($i=0, $n=count( $options ); $i < $n; $i++ )
		{
                    $html .= "\n\t<input type=\"radio\" name=\"".$control_name.'['.$name.']'."\" id=\"$control_name$name$i\" value=\"".$options[$i]->value."\" ".$options[$i]->checked."/>";
                    $html .= "\n\t<label for=\"".$control_name.$name.$i."\">".$options[$i]->text."</label>";
		}
		
                $html .= "\n";
		return $html;
                		
	}
}
